home *** CD-ROM | disk | FTP | other *** search
- /* VERY QUICK AND ULTRA-DIRTY DEMO USING XLIB */
-
- /* Simple Demo of MODE X Split screen and panning */
- /* Compile using Turbo C and Tasm */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <ctype.h>
- #include <alloc.h>
- #include <dos.h>
- #include "Xlib_all.h"
-
- #define MAX_OBJECTS 10
-
- typedef struct {
- int X,Y,Width,Height,XDir,YDir,XOtherPage,YOtherPage;
- char far * Image;
- char far * bg;
- char far * bgOtherPage;
- } AnimatedObject;
-
- AnimatedObject objects[MAX_OBJECTS];
- int object_count=0;
-
- static char bm[] = {4,12,
- /* plane 0 */
- 2,2,2,2,2,1,1,1,2,1,1,1,2,3,3,1,
- 2,0,0,3,2,0,0,3,2,0,0,3,2,0,0,3,
- 2,3,3,1,2,1,1,1,2,1,1,1,2,2,2,2,
- /* plane 1 */
- 2,2,2,2,1,1,1,1,1,1,1,1,1,3,3,1,
- 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,
- 1,3,3,1,1,1,1,1,1,1,1,1,2,2,2,2,
- /* plane 2 */
- 2,2,2,2,1,1,1,1,1,1,1,1,1,3,3,1,
- 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,
- 1,3,3,1,1,1,1,1,1,1,1,1,2,2,2,2,
- /* plane 3 */
- 2,2,2,2,1,1,1,2,1,1,1,2,1,3,3,2,
- 3,0,0,2,3,0,0,2,3,0,0,2,3,0,0,2,
- 1,3,3,2,1,1,1,2,1,1,1,2,2,2,2,2};
-
- static char bm2[] = {4,12,
- /* plane 0 */
- 2,2,2,2,2,4,4,4,2,4,4,4,2,2,2,4,
- 2,0,0,2,2,0,0,2,2,0,0,2,2,0,0,2,
- 2,2,2,4,2,4,4,4,2,4,4,4,2,2,2,2,
- /* plane 1 */
- 2,2,2,2,4,4,4,4,4,4,4,4,4,2,2,4,
- 4,0,0,4,4,0,0,4,4,0,0,4,4,0,0,4,
- 4,2,2,4,4,4,4,4,4,4,4,4,2,2,2,2,
- /* plane 2 */
- 2,2,2,2,4,4,4,4,4,4,4,4,4,2,2,4,
- 4,0,0,4,4,0,0,4,4,0,0,4,4,0,0,4,
- 4,2,2,4,4,4,4,4,4,4,4,4,2,2,2,2,
- /* plane 2 */
- 2,2,2,2,4,4,4,2,4,4,4,2,4,2,2,2,
- 2,0,0,2,2,0,0,2,2,0,0,2,2,0,0,2,
- 4,2,2,2,4,4,4,2,4,4,4,2,2,2,2,2};
-
-
- /* initialize a new object */
- void init_object(int x,int y,int width, int height, int xdir, int ydir,
- char far * image){
- int i;
- objects[object_count].X = objects[object_count].XOtherPage = x;
- objects[object_count].Y = objects[object_count].YOtherPage = y;
- objects[object_count].Width = width;
- objects[object_count].Height = height;
- objects[object_count].XDir = xdir;
- objects[object_count].YDir = ydir;
- objects[object_count].Image = image;
- objects[object_count].bg = (char far *) farmalloc(4*width*height+20);
- objects[object_count].bgOtherPage = (char far *) farmalloc(4*width*height+20);
- x_get_pbm(x,y,(unsigned)width,height,VisiblePageOffs,
- objects[object_count].bg);
- x_get_pbm(x,y,(unsigned)width,height,HiddenPageOffs,
- objects[object_count].bgOtherPage);
- object_count++;
- }
-
- /* Move the specified object, bouncing at the edges of the screen and
- remembering where the object was before the move for erasing next time */
- void MoveObject(AnimatedObject * ObjectToMove) {
- int X, Y;
- char far *cptr;
- X = ObjectToMove->X + ObjectToMove->XDir;
- Y = ObjectToMove->Y + ObjectToMove->YDir;
- if ((X < 0) || (X > (ScrnLogicalPixelWidth-((ObjectToMove->Width)<<2)))) {
- ObjectToMove->XDir = -ObjectToMove->XDir;
- X = ObjectToMove->X + ObjectToMove->XDir;
- }
- if ((Y < 0) || (Y > (ScrnLogicalHeight-ObjectToMove->Height))) {
- ObjectToMove->YDir = -ObjectToMove->YDir;
- Y = ObjectToMove->Y + ObjectToMove->YDir;
- }
- /* Remember previous location for erasing purposes */
- ObjectToMove->XOtherPage = ObjectToMove->X;
- ObjectToMove->YOtherPage = ObjectToMove->Y;
- ObjectToMove->X = X; /* set new location */
- ObjectToMove->Y = Y;
- cptr = ObjectToMove->bg;
- ObjectToMove->bg = ObjectToMove->bgOtherPage;
- ObjectToMove->bgOtherPage = cptr;
- }
-
- void animate(){
- int i;
- for(i=object_count-1;i>=0;i--){
- x_put_pbm(objects[i].XOtherPage,objects[i].YOtherPage,
- HiddenPageOffs,objects[i].bgOtherPage);
- }
- for(i=0;i<object_count;i++){
- MoveObject(&objects[i]);
-
- x_get_pbm(objects[i].X,objects[i].Y,
- (unsigned)objects[i].Width,objects[i].Height,HiddenPageOffs,
- objects[i].bg);
- x_put_masked_pbm(objects[i].X,objects[i].Y,HiddenPageOffs,
- objects[i].Image);
- }
- }
-
- void clear_objects(){
- int i;
- for(i=object_count-1;i>=0;i--){
- x_put_pbm(objects[i].XOtherPage,objects[i].YOtherPage,
- HiddenPageOffs,objects[i].bgOtherPage);
- }
- }
-
- /* draw a coloured rectangle */
-
- void draw_rect(int x, int y, int width, int color, unsigned int page){
- int i,j;
- for (j=0;j<width;j++)
- for(i=0;i<width;i++){
- x_put_pix(x+j,y+i,page,color);
- }
- }
-
-
- int textwindow_x=0,textwindow_y=0;
- char far * pal,far * pal2;
- char palscrolldir=1;
- char far * newfnt;
-
-
- void textwindow(int Margin){
- int x0=0+Margin;
- int y0=0+Margin;
- int x1=ScrnPhysicalPixelWidth-Margin;
- int y1=ScrnPhysicalHeight-Margin;
- x_rect_fill(x0, y0, x1,y1,Page0_Offs,1);
- x_line(x0,y0,x1,y0,2,Page0_Offs);
- x_line(x0,y1,x1,y1,2,Page0_Offs);
- x_line(x0,y0,x0,y1,2,Page0_Offs);
- x_line(x1,y0,x1,y1,2,Page0_Offs);
- x_line(x0+2,y0+2,x1-2,y0+2,2,Page0_Offs);
- x_line(x0+2,y1-2,x1-2,y1-2,2,Page0_Offs);
- x_line(x0+2,y0+2,x0+2,y1-2,2,Page0_Offs);
- x_line(x1-2,y0+2,x1-2,y1-2,2,Page0_Offs);
- textwindow_x=x0;
- textwindow_y=y0;
-
- }
-
-
- void wait_for_keypress(){
- while(kbhit()) getch();
- palscrolldir^=1;
- do {
- x_rot_pal_struc(pal,palscrolldir);
- x_put_pal_struc(pal);
- } while (!kbhit());
- while(kbhit()) getch();
-
- }
-
-
- void intro_1(){
- x_set_rgb(1,40,40,40); /* BG Gray */
- x_set_rgb(2,63,63,0); /* Bright Yellow */
- x_set_rgb(3,63,0,0); /* Bright Red */
- x_set_rgb(4,0,63,0); /* Bright Green */
- x_set_rgb(5,0,0,63); /* Bright Blue */
- x_set_rgb(6,0,0,28); /* Dark Blue */
- x_set_rgb(7,0,28,0); /* Dark Green */
- x_set_rgb(8,28,0,0); /* Dark red */
- x_set_rgb(9,0,0,38); /* Med Blue */
-
- textwindow(20);
- x_set_font(1);
- x_printf(textwindow_x+54,textwindow_y+4,Page0_Offs,6," XLIB Version 3.0");
- x_printf(textwindow_x+53,textwindow_y+3,Page0_Offs,2," XLIB Version 3.0");
- x_set_font(0);
- x_printf(textwindow_x+24,textwindow_y+18,Page0_Offs,6," Not the Unix version");
- x_printf(textwindow_x+23,textwindow_y+17,Page0_Offs,2," Not the Unix version");
-
- x_printf(textwindow_x+24,168,Page0_Offs,6," Press any key to continue");
- x_printf(textwindow_x+23,167,Page0_Offs,2," Press any key to continue");
- }
-
- void subsequent_page(){
- textwindow(20);
- x_set_font(1);
- x_printf(textwindow_x+54,textwindow_y+4,Page0_Offs,6," XLIB Version 3.0");
- x_printf(textwindow_x+53,textwindow_y+3,Page0_Offs,2," XLIB Version 3.0");
- x_set_font(0);
- x_printf(textwindow_x+24,168,Page0_Offs,6," Press any key to continue");
- x_printf(textwindow_x+23,167,Page0_Offs,2," Press any key to continue");
- }
-
- void load_user_fonts(){
- FILE *f;
- f=fopen("6x8b.fnt","rb");
- /* read char by char as fread wont read to far pointers in small model */
- { int i; char c;
- for (i=0;i<256*8+4;i++){
- fread(&c,1,1,f);
- *(newfnt+i)=c;
- }
- }
-
- fclose(f);
- x_register_userfont(newfnt);
-
- }
-
- void main(){
- int i, j, xinc, yinc, Margin;
- char ch;
- WORD curr_x=0, curr_y=0;
-
- pal = (char far *) farmalloc(256*3);
- pal2 = (char far *) farmalloc(256*3);
- newfnt = (char far *) farmalloc(256*16+4);
-
- /* setup Mode X 320x240x256 with a logical width of ~ 500 pixels */
- /* we actually get 496 due to the fact that the width must be divisible */
- /* by 8 */
-
-
- /* INITIALIZE XLIB */
-
- x_set_mode(X_MODE_360x200,500); /* actually is set to 496 */
- x_set_splitscreen(ScrnPhysicalHeight-60); /* split screen 60 pixels high */
- x_set_doublebuffer(220);
- x_text_init();
- x_hide_splitscreen();
-
- /* DRAW BACKGROUND LINES */
-
- for(j=0;j<ScrnPhysicalHeight;j++){
- x_line(0,j,ScrnLogicalPixelWidth,j,16+(j%239),Page0_Offs);
- }
-
-
- x_get_pal_struc(pal, 240,16);
- load_user_fonts();
-
- getch();
- intro_1();
- x_set_font(2);
-
- x_printf(textwindow_x+5,50 ,Page0_Offs,9, " Hi folks, this is yet another FREEWARE Mode X ");
- x_printf(textwindow_x+5,50+8 ,Page0_Offs,9, " graphics library. It is by no means complete, ");
- x_printf(textwindow_x+5,50+16,Page0_Offs,9, " but I believe it contains a rich enough set of ");
- x_printf(textwindow_x+5,50+24,Page0_Offs,9, " functions to achieve it's design goal - To be ");
- x_printf(textwindow_x+5,50+32,Page0_Offs,9, " a game development oriented library for ");
- x_printf(textwindow_x+5,50+40,Page0_Offs,9, " Borland TC/BC/BC++ and TASM programmers. ");
-
- x_printf(textwindow_x+5,50+48,Page0_Offs,9, " This library comes with TASM and C sources and ");
- x_printf(textwindow_x+5,50+56,Page0_Offs,9, " was inspired by DDJ Graphics column and many ");
- x_printf(textwindow_x+5,50+64,Page0_Offs,9, " INTERNET an USENET Authors who unlike the ");
- x_printf(textwindow_x+5,50+72,Page0_Offs,9, " majority of programmers, (you know who you are!)");
- x_printf(textwindow_x+5,50+80,Page0_Offs,9, " willingly share their code and ideas with others.");
-
- x_printf(textwindow_x+5,50+88,Page0_Offs,9, " I can't afford to nor do I want to copyright ");
- x_printf(textwindow_x+5,50+96,Page0_Offs,9, " this code, but if you use it, some credit would ");
- x_printf(textwindow_x+5,50+104,Page0_Offs,9," be appreciated. ");
-
- wait_for_keypress();
-
- subsequent_page();
- x_set_font(0);
- x_printf(textwindow_x+24,textwindow_y+18,Page0_Offs,6,"Supported 256 colour resolutions.");
- x_printf(textwindow_x+23,textwindow_y+17,Page0_Offs,3,"Supported 256 colour resolutions.");
- x_set_font(2);
- x_printf(textwindow_x+5,50 ,Page0_Offs,9, " 320x200 Standard for games ~ 4 pages");
- x_printf(textwindow_x+5,50+8 ,Page0_Offs,9, " 320x240 DDJ Mode X square pixels ~ 3.5 pages");
- x_printf(textwindow_x+5,50+16,Page0_Offs,9, " 360x200 My favourite for games ~ 3 pages ");
- x_printf(textwindow_x+5,50+24,Page0_Offs,9, " 360x240 ~ 2.8 pages");
- x_printf(textwindow_x+5,50+32,Page0_Offs,9, " 320x400 ~ 2 pages ");
- x_printf(textwindow_x+5,50+40,Page0_Offs,9, " 320x480 All subsequent modes support less than");
- x_printf(textwindow_x+5,50+48,Page0_Offs,9, " 360x400 two pages.");
- x_printf(textwindow_x+5,50+56,Page0_Offs,9, " 360x480 ");
- x_printf(textwindow_x+5,50+64,Page0_Offs,9, "All modes may not be suitable for your requirements");
- x_printf(textwindow_x+5,50+72,Page0_Offs,9, "remember that standard vga only supports 64K of ");
- x_printf(textwindow_x+5,50+80,Page0_Offs,9, "video RAM and these modes are specifically designed");
- x_printf(textwindow_x+5,50+88,Page0_Offs,9, "for standard VGA cards and monitors.");
- x_printf(textwindow_x+5,50+96,Page0_Offs,2, " We are currently in 320x200 mode.");
-
-
-
-
- wait_for_keypress();
-
- subsequent_page();
- x_printf(textwindow_x+24,textwindow_y+18,Page0_Offs,6," Text display functions.");
- x_printf(textwindow_x+23,textwindow_y+17,Page0_Offs,3," Text display functions.");
- x_set_font(2);
- x_printf(textwindow_x+5,50 ,Page0_Offs,9, " Functions for text printing are provided that ");
- x_printf(textwindow_x+5,50+8 ,Page0_Offs,9, " support the VGA ROM 8x14 and 8x8 fonts as well ");
- x_printf(textwindow_x+5,50+16,Page0_Offs,9, " as user defined fonts (like this 6x8 font). ");
- x_printf(textwindow_x+5,50+24,Page0_Offs,9, " Furthermore a function similar to printf is ");
- x_printf(textwindow_x+5,50+32,Page0_Offs,9, " provided that provides formatted text output. ");
- x_set_font(1);
- x_printf(textwindow_x+5,50+42,Page0_Offs,2, " ROM 8x14");
- x_set_font(0);
- x_printf(textwindow_x+5,50+46,Page0_Offs,2, " ROM 8x8");
- x_set_font(2);
- x_printf(textwindow_x+5,50+46,Page0_Offs,2, " User defined 6x8 ");
-
- wait_for_keypress();
-
-
- subsequent_page();
- x_printf(textwindow_x+24,textwindow_y+18,Page0_Offs,6," Advanced screen functions");
- x_printf(textwindow_x+23,textwindow_y+17,Page0_Offs,3," Advanced screen functions");
- x_set_font(2);
- x_printf(textwindow_x+5,50 ,Page0_Offs,9, " The library supports virtual screens larger ");
- x_printf(textwindow_x+5,50+8 ,Page0_Offs,9, " than the physical screen, panning of such ");
- x_printf(textwindow_x+5,50+16,Page0_Offs,9, " screens, and a split screen option. All of the ");
- x_printf(textwindow_x+5,50+24,Page0_Offs,9, " above functions can be used together or ");
- x_printf(textwindow_x+5,50+32,Page0_Offs,9, " in isolation, and in the lower resolutions ");
- x_printf(textwindow_x+5,50+40,Page0_Offs,9, "double buffering can also be accomplished. ");
-
- x_rect_fill(0, 0, ScrnPhysicalPixelWidth,60,SplitScrnOffs,5);
- x_line(0,0,ScrnPhysicalPixelWidth,0,2,SplitScrnOffs);
- x_set_font(1);
- x_printf(10,10,SplitScrnOffs,2, " This is a split screen, Tops for scores.");
- x_set_font(0);
- for (i=ScrnPhysicalHeight;i>ScrnPhysicalHeight-60;i--){
- x_adjust_splitscreen(i);
- }
- x_printf(10,25,SplitScrnOffs,2, " Even better for scrolling games etc.");
-
-
- x_cp_vid_rect(0,0,ScrnLogicalPixelWidth,ScrnLogicalHeight,0,0,
- VisiblePageOffs,HiddenPageOffs,
- ScrnLogicalPixelWidth,ScrnLogicalPixelWidth);
-
- getch();
-
- curr_x=curr_y=0;
-
-
- init_object(60,90,4, 12, -1, 1, MK_FP(FP_SEG(bm2),FP_OFF(bm2)));
- init_object(30,30,4, 12, 1, 1, MK_FP(FP_SEG(bm),FP_OFF(bm)));
- init_object(80,120,4, 12, 2, 1, MK_FP(FP_SEG(bm),FP_OFF(bm)));
- init_object(300,200,4, 12, 1, -2, MK_FP(FP_SEG(bm),FP_OFF(bm)));
- init_object(360,30,4, 12, -1, -1, MK_FP(FP_SEG(bm),FP_OFF(bm)));
- init_object(360,10,4, 12, -2, 2, MK_FP(FP_SEG(bm),FP_OFF(bm)));
-
- while (!kbhit()){
- animate();
- if (objects[0].X>=curr_x+ScrnPhysicalPixelWidth-32 &&
- curr_x < MaxScrollX) curr_x++;
- else if (objects[0].X < curr_x+16 && curr_x > 0) curr_x--;
- if (objects[0].Y>=curr_y+ScrnPhysicalHeight-92 &&
- curr_y < MaxScrollY) curr_y++;
- else if (objects[0].Y < curr_y+16 && curr_y > 0) curr_y--;
- x_page_flip(curr_x,curr_y);
- }
-
- clear_objects();
- x_page_flip(curr_x,curr_y);
-
-
- x_set_start_addr(0,0);
-
- /* if (VisiblePageIdx)
- x_page_flip(0,0);
- */
-
- for (j=0;j<4;j++){
- x_hide_splitscreen();
- delay(100);
- x_show_splitscreen();
- delay(100);
- }
-
-
- for (i=ScrnPhysicalHeight-60;i<=ScrnPhysicalHeight;i++){
- x_adjust_splitscreen(i);
- }
-
-
- subsequent_page();
- x_printf(textwindow_x+24,textwindow_y+18,Page0_Offs,6," Palette functions.");
- x_printf(textwindow_x+23,textwindow_y+17,Page0_Offs,3," Palette functions.");
- x_set_font(2);
- x_printf(textwindow_x+5,50 ,Page0_Offs,9, " A number of palette manipulation functions ");
- x_printf(textwindow_x+5,50+8 ,Page0_Offs,9, " are provided, you have already seen some of ");
- x_printf(textwindow_x+5,50+16,Page0_Offs,9, " them in action. Another common operation is ");
- x_printf(textwindow_x+5,50+24,Page0_Offs,9, " palette fading. ");
-
-
- i=0;
- ch=255;
- while (x_cpcontrast_pal_struc(pal, pal2,ch-=1)){
- x_put_pal_struc(pal2);
- x_rot_pal_struc(pal,palscrolldir);
- i++;
- };
- for (j=0;j<i;j++){
- x_cpcontrast_pal_struc(pal, pal2,ch+=1);
- x_put_pal_struc(pal2);
- x_rot_pal_struc(pal,palscrolldir);
- };
- wait_for_keypress();
-
- subsequent_page();
- x_printf(textwindow_x+24,textwindow_y+18,Page0_Offs,6," NEW Version 3.0 Functions!");
- x_printf(textwindow_x+23,textwindow_y+17,Page0_Offs,3," NEW Version 3.0 Functions!");
- x_set_font(2);
- x_printf(textwindow_x+5,50 ,Page0_Offs,9, " NEW functions not demonstrated here include: ");
- x_printf(textwindow_x+5,50+8 ,Page0_Offs,9, " - RLE data compression ");
- x_printf(textwindow_x+5,50+16,Page0_Offs,9, " - FAST Compiled masked bitmaps (see demo2) ");
- x_printf(textwindow_x+5,50+24,Page0_Offs,9, " - Hardware detection ");
-
- wait_for_keypress();
-
- subsequent_page();
- x_printf(textwindow_x+24,textwindow_y+18,Page0_Offs,6," PLEASE... ");
- x_printf(textwindow_x+23,textwindow_y+17,Page0_Offs,3," PLEASE...");
- x_set_font(2);
- x_printf(textwindow_x+5,50 ,Page0_Offs,9, " Please mention my name in programs that use XLIB ");
- x_printf(textwindow_x+5,50+8 ,Page0_Offs,9, " just to make me feel it was worth the effort. .");
- x_printf(textwindow_x+5,50+16,Page0_Offs,9, " If you have any bug to report please feel free to ");
- x_printf(textwindow_x+5,50+24,Page0_Offs,9, " mail me a message. Any hints, suggestions and ");
- x_printf(textwindow_x+5,50+32,Page0_Offs,9, " contributions are welcome and encouraged. ");
-
- x_printf(textwindow_x+5,50+56,Page0_Offs,9, " I have contributed this code to the public domain ");
- x_printf(textwindow_x+5,50+64,Page0_Offs,9, " please respect my wishes and leave it there. ");
-
- x_printf(textwindow_x+5,50+80,Page0_Offs,9, " Finally I hope you all find this stuff useful,");
- x_printf(textwindow_x+5,50+96,Page0_Offs,9, " Themie Gouthas - EGG@DSTOS3.DSTO.GOV.AU");
-
- wait_for_keypress();
-
- x_text_mode();
- }
-